package org.shopen.puremvc.view
{
	import flash.display.DisplayObject;
	
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.puremvc.ApplicationConstants;
	import org.shopen.puremvc.model.vo.FileType;
	import org.shopen.puremvc.model.vo.SaveInfo;
	import org.shopen.puremvc.view.components.dialogs.SaveWindow;
	import org.shopen.puremvc.view.components.dialogs.events.DialogEvent;
	import org.shopen.puremvc.view.components.dialogs.events.SaveDialogEvent;

	public class SaveWindowMediator extends Mediator
	{
		public static const NAME : String = "SaveWindowMediator";
		
		protected var saveWindow : SaveWindow = null;
		
		protected function get application() : DisplayObject
		{
			var result : DisplayObject = viewComponent as DisplayObject;
			return result;
		}
		
		public function SaveWindowMediator(viewComponent:Object=null)
		{
			super(SaveWindowMediator.NAME, viewComponent);
		}
		
		override public function getMediatorName():String
		{
			return SaveWindowMediator.NAME;
		}
		
		override public function onRemove():void
		{
			removeSaveWindow();
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationConstants.SHOW_SAVING_WINDOW,
					ApplicationConstants.HIDE_SAVING_WINDOW];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var body : Object = notification.getBody();
			switch(notification.getName())
			{
				case ApplicationConstants.SHOW_SAVING_WINDOW:
					var saveInfo : SaveInfo = body as SaveInfo;
					showSaveWindow(saveInfo);
					break;
				case ApplicationConstants.HIDE_SAVING_WINDOW:
					removeSaveWindow();
					break;
			}
		}
		
		private function showSaveWindow(saveInfo : SaveInfo = null) : void
		{
			// Make sure only one about window is shown
			if (saveWindow == null)
			{
				saveWindow = createNewWindow(saveInfo);
				PopUpManager.addPopUp(saveWindow, application, true);
			}
			
			PopUpManager.bringToFront(saveWindow);
			PopUpManager.centerPopUp(saveWindow);
		}
		
		private function createNewWindow(saveInfo : SaveInfo = null) : SaveWindow
		{
			var window : SaveWindow = new SaveWindow();
			
			
			if (saveInfo != null)
			{
				window.filename = saveInfo.filename;
				window.filetypeProvider = saveInfo.filetypes;
				window.filetypeLabelField = "label";
			}
			
			window.addEventListener(CloseEvent.CLOSE, windowCloseListener);
			window.addEventListener(SaveDialogEvent.SAVE, saveEventListener);
			window.addEventListener(DialogEvent.CANCEL, cancelEventListener);
			
			return window;
		}
		
		private function windowCloseListener(event : CloseEvent) : void
		{
			if (saveWindow == event.target)
			{
				saveWindow.removeEventListener(CloseEvent.CLOSE, windowCloseListener);
				
				sendCancel();
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		private function saveEventListener(event : SaveDialogEvent) : void
		{
			if (saveWindow == event.target)
			{
				saveWindow.removeEventListener(SaveDialogEvent.SAVE, saveEventListener);
				
				var saveInfo : SaveInfo = new SaveInfo();
				saveInfo.filename = saveWindow.filename;
				saveInfo.selectedFileType = saveWindow.currentFiletype as FileType;
				sendSave(saveInfo);
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		private function cancelEventListener(event : DialogEvent) : void
		{
			if (saveWindow == event.target)
			{
				saveWindow.removeEventListener(DialogEvent.CANCEL, cancelEventListener);
				sendCancel();
			}
			else
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR);
			}
		}
		
		protected function sendSave(saveInfo : SaveInfo) : void
		{
			sendNotification(ApplicationConstants.SAVE, saveInfo);
		}
		
		protected function sendCancel() : void
		{
			sendNotification(ApplicationConstants.CANCEL_SAVING_REQUEST);
		}
		
		protected function removeSaveWindow() : void
		{
			if (saveWindow != null)
			{
				if (saveWindow.hasEventListener(CloseEvent.CLOSE))
				{
					saveWindow.removeEventListener(CloseEvent.CLOSE, windowCloseListener);
				}
				if (saveWindow.hasEventListener(DialogEvent.CANCEL))
				{
					saveWindow.removeEventListener(DialogEvent.CANCEL, cancelEventListener);
				}
				if (saveWindow.hasEventListener(SaveDialogEvent.SAVE))
				{
					saveWindow.removeEventListener(SaveDialogEvent.SAVE, saveEventListener);
				}
				PopUpManager.removePopUp(saveWindow);
				saveWindow = null;
			}
		}
		
	}
}